package dao;

import dto.RoleFunctions;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import utils.SQLServerDB;

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
/**
 *
 * @author vanchung07
 */
public class RoleFunctionsDAO {

    private String lastError;
    private final String SQL_CREATE = "INSERT INTO RoleFunctions Values(?, ?)";
    private final String SQL_READBYROLEID = "SELECT * FROM RoleFunctions WHERE RoleID = ?";
    private final String SQL_DELETE = "DELETE FROM RoleFunctions WHERE RoleID = ? AND FunctionID = ?";
    private Connection conn;

    public boolean create(RoleFunctions rf) {
        conn = SQLServerDB.getConn();
        try {
            PreparedStatement pr = conn.prepareStatement(SQL_CREATE);
            pr.setInt(1, rf.getRoleId());
            pr.setInt(2, rf.getFunctionId());
            if (pr.executeUpdate() > 0) {
                setLastError("Create Successful!");
                return true;
            } else {
                setLastError("Error: Create Failed!");
            }
        } catch (SQLException ex) {
            setLastError("Error: " + ex.getMessage());
        } finally {
            SQLServerDB.closeConn(conn);
        }
        return false;
    }

    public ArrayList<RoleFunctions> readByRoleId(int id) {
        conn = SQLServerDB.getConn();
        ArrayList<RoleFunctions> list = new ArrayList<>();
        try {
            PreparedStatement pr = conn.prepareStatement(SQL_READBYROLEID);
            pr.setInt(1, id);
            ResultSet rs = pr.executeQuery();
            while (rs.next()) {
                RoleFunctions rf = new RoleFunctions();
                rf.setRoleId(rs.getInt("RoleID"));
                rf.setFunctionId(rs.getInt("FunctionID"));
                list.add(rf);
            }
        } catch (SQLException ex) {
            setLastError("Error: " + ex.getMessage());
        } finally {
            SQLServerDB.closeConn(conn);
        }
        return list;
    }

    public boolean delete(RoleFunctions rf) {
        conn = SQLServerDB.getConn();
        try {
            PreparedStatement pr = conn.prepareStatement(SQL_DELETE);
            pr.setInt(1, rf.getRoleId());
            pr.setInt(2, rf.getFunctionId());
            if (pr.executeUpdate() > 0) {
                setLastError("Delete Successful!");
                return true;
            } else {
                setLastError("Error: Delete Failed!");
            }
        } catch (SQLException ex) {
            setLastError("Error: " + ex.getMessage());
        } finally {
            SQLServerDB.closeConn(conn);
        }
        return false;
    }

    public boolean isExist(RoleFunctions rf) {
        ArrayList<RoleFunctions> list = readByRoleId(rf.getRoleId());
        if (list.contains(rf)) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * @return the lastError
     */
    public String getLastError() {
        return lastError;
    }

    /**
     * @param lastError the lastError to set
     */
    public void setLastError(String lastError) {
        this.lastError = lastError;
    }
    
    public static void main(String[] args) {
        RoleFunctionsDAO dao = new RoleFunctionsDAO();
        RoleFunctions rf = new RoleFunctions();
        rf.setRoleId(3);
        rf.setFunctionId(6);
        System.out.println(dao.isExist(rf));
        System.out.println(dao.getLastError());
    }
}
